package com.youhong.teethcare.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.support.v7.widget.AppCompatCheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;


import com.youhong.teethcare.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 周末选择适配器
 *
 * @author Administrator
 */
public class WeekendAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private String[] weekarray;
    private HashMap<Integer, Boolean> hashMap;

    public WeekendAdapter(Context context, ArrayList<Integer> postions) {
        this.inflater = LayoutInflater.from(context);
        this.weekarray = context.getResources().getStringArray(R.array.weekarray2);
        hashMap = new HashMap<Integer, Boolean>();
        for (int i = 0; i < 7; i++) {
            if (postions.contains(i)) {

                hashMap.put(i, true);
            } else {
                hashMap.put(i, false);
            }
        }

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return weekarray.length;
    }

    @Override
    public String getItem(int arg0) {
        // TODO Auto-generated method stub
        return weekarray[arg0];
    }

    public void setPostion(ArrayList<Integer> postion) {
        for (int i = 0; i < 7; i++) {
            if (postion.contains(i)) {
                hashMap.put(i, true);
            } else {
                hashMap.put(i, false);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    public ArrayList<Integer> getCheckItem() {
        ArrayList<Integer> lists = new ArrayList<Integer>();
        for (int i = 0; i < 7; i++) {
            if (hashMap.get(i)) {
                lists.add(i);
            }
        }
        return lists;
    }

    @Override
    public View getView(final int postion, View view, ViewGroup arg2) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.weekenditem, null);
            holder.checkBox = view.findViewById(R.id.checkBox_week_item);
            holder.tv = (TextView) view.findViewById(R.id.tv_week_item);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv.setText(weekarray[postion]);
        holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                hashMap.put(postion, arg1);
            }
        });
        holder.checkBox.setChecked(hashMap.get(postion));
        return view;
    }

    class ViewHolder {
        public AppCompatCheckBox checkBox;
        public TextView tv;
    }
}
